package com.e_manager.soft.emanager.service.impl;

import com.e_manager.soft.emanager.entity.*;
import com.e_manager.soft.emanager.entity.parameter.MstMessageParameter;
import com.e_manager.soft.emanager.entity.parameter.StatisticHourParameter;
import com.e_manager.soft.emanager.idao.MstMessageDao;
import com.e_manager.soft.emanager.service.MstMessageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 徐玲玉
 * 2018/3/21 0013
 */
@Service
public class MstMessageServiceImpl implements MstMessageService {
    @Autowired
    private MstMessageDao mstMessageDao;

    /**
     * 插入报文信息
     *
     * @param mstMessage
     */
    @Override
    public void insertMstMessage(MstMessage mstMessage) {

        if (mstMessage != null) {
            mstMessageDao.insertMstMessage(mstMessage);
        }
    }

    /**
     * 根据设备编号查询信鸽token
     *
     * @param equipment
     * @return
     */
    @Override
    public List<User> queryXinToken(Equipment equipment) {
        List<User> findXinList = mstMessageDao.queryXinToken(equipment);
        return findXinList;
    }

    @Override
    public PageInfo<MstMessage> findMessageByPage(MstMessageParameter mp) {
        /**
         * @author lip
         * @description 报文列表查询
         * @method findMessage
         * @param []
         * @returnType java.util.List<com.e_manager.soft.emanager.entity.MstMessage>
         * @date 2018/3/16 0016 上午 9:53
         */
        PageHelper.startPage(mp.getPageNumber(), mp.getPageSize());
        List<MstMessage> messageList = mstMessageDao.findMessageByPage(mp);
        return new PageInfo<MstMessage>(messageList);
    }

    /**
     * web按月份统计
     *
     * @return
     */
    @Override
    public Statistics queryStatistics() {
        //初始化对象
        List<StatisticsReturnList> list = null;
        Statistics statistics = new Statistics();
        List<String> monthList = new ArrayList<String>();
        List<String> monthcountList = new ArrayList<String>();
        try {
            //取数据库里数据
            list = mstMessageDao.queryStatistics();
            //放到bean类里
            list.stream().filter(a -> a != null).forEach(a -> {
                monthList.add(a.getMonth());
                monthcountList.add(a.getNum());
            });
            statistics.setMonthList(monthList);
            statistics.setMonthcountList(monthcountList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return statistics;
    }

    /**
     * 安卓按天统计
     *
     * @param statisticHourParameter
     * @return
     */
    @Override
    public StatisticsHour queryHourStatistics(StatisticHourParameter statisticHourParameter) {
        //初始化对象
        List<StatisticsHourRetrunList> list = null;
        StatisticsHour statisticsHour = new StatisticsHour();
        List<String> dayHourList = new ArrayList<String>();
        List<String> temperatureCountList = new ArrayList<String>();
        List<String> humidityCountList = new ArrayList<String>();
        try {
            list = mstMessageDao.queryHourStatistics(statisticHourParameter);
            //放到bean类里
            if ("0".equals(statisticHourParameter.getTypeCode())) {
                list.stream().filter(a -> a != null).forEach(a -> {
                    dayHourList.add(a.getHour());
                    temperatureCountList.add(a.getTemperaturenum());
                });
                statisticsHour.setDayHourList(dayHourList);
                statisticsHour.setTemperatureCountList(temperatureCountList);
            } else {
                list.stream().filter(a -> a != null).forEach(a -> {
                    dayHourList.add(a.getHour());
                    humidityCountList.add(a.getHumiditynum());
                });
                statisticsHour.setDayHourList(dayHourList);
                statisticsHour.setHumidityCountList(humidityCountList);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return statisticsHour;
    }
}
